package com.ophiux.utils.common;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class ShiftUtils<T> {
	/**
	 * 将Map集合转换为object 对象
	 * 
	 * @param map
	 * @return
	 */
	public static <t> Object mapToObj(Map<String, Object> map,
			Class classT) {
		try {
			return transfer(map, classT.newInstance());
		} catch (InstantiationException ex) {
			ex.printStackTrace();
			System.out.println("what the fuck1?");
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
			System.out.println("what the fuck2?");
		}
		return null;
	}

	private static <t> Object transfer(Map<String, Object> map, Object obj) {

		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();

			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				if (map.containsKey(key)) {
					Object value = map.get(key);
					// 得到property对应的setter方法
					Method setter = property.getWriteMethod();
					try {
						setter.invoke(obj, value);
					} catch (IllegalArgumentException ex) {
						System.out.println("what the fuck?");
					}
				}
			}
		} catch (Exception ex) {
			System.out.println("what the fuck?");
		}

		return  obj;
	}

	
	/**
	 * 将对象转换为map
	 * @param object
	 * @return
	 */
	public static Map objToMap(Object object) {
		if (object == null) {
			return null;
		}
		TreeMap<String, Object> map = new TreeMap<String, Object>();
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo
					.getPropertyDescriptors();
			for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
				// 过滤class属性
				if (key.equals("class")) {
					continue;
				}

				// 得到property对应的getter方法
				Method getter = property.getReadMethod();
				Object value = getter.invoke(object);

				map.put(key, value);
			}
		} catch (Exception e) {
			System.out.println("transBean2Map Error " + e);
		}
		return map;
	}

	/**
	 * 将对象转换为新的对象
	 * @param obj 转换前的对象
	 * @param Class 转换后的对象类
	 * @return
	 */
	public static Object objToNewObj(Object obj ,Class classObj) {
		Map objMap = objToMap(obj);
		Object returnobj = mapToObj(objMap, classObj);
		return returnobj;
	}
	
	
	
	/**
	 * 将对象转换为新的对象
	 * @param obj 转换前的对象
	 * @param Class 转换后的对象类
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List objToNewObjList(List objS ,Class classObj) {
		List obS = new ArrayList();
		for (Object obj : objS) {
			Map objMap = objToMap(obj);
			Object returnobj = mapToObj(objMap, classObj);
			obS.add(returnobj);
		}
		return obS;
	}
	

	public static void main(String[] args) {
//		MapUtils util = new MapUtils();
//		util.objToMap(patient);
//
//		PatientInfo pati = (PatientInfo) mapToObj(util.objToMap(patient),
//				PatientInfo.class);
	}
	
}
